<?php
class Candidate_address_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('candidate_address.*, country.name AS countryname, state.name AS statename, state.shortname AS stateshortname');
    	$this->db->from('candidate_address');
    	$this->db->join('country', 'candidate_address.idcountry = country.id', 'left');
    	$this->db->join('state', 'candidate_address.idstate = state.id', 'left');
    	
    	if (isset($data['idcandidate'])) {
    		$this->db->where('candidate_address.idcandidate', $data['idcandidate']);
    	}
    	
    	if (isset($data['idaddress']) && is_numeric($data['idaddress'])) {
    		$this->db->where('candidate_address.id', $data['idaddress']);
    	}
    	
    	$this->db->order_by('candidate_address.id');
    	
    	return $this->db->get();
    }
    
	function insert($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'neighborhood' => $data['neighborhood'],
		   'city' => $data['city'],
		   'address' => $data['address'],
		   'number' => $data['number'],
		   'additional' => $data['additional'],
		   'zipcode' => $data['zipcode'],
		   'idcandidate' => $data['idcandidate']
		);
		
		return $this->db->insert('candidate_address', $data_insert);
    }
    
	function update($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'neighborhood' => $data['neighborhood'],
		   'city' => $data['city'],
		   'address' => $data['address'],
		   'number' => $data['number'],
		   'additional' => $data['additional'],
		   'zipcode' => $data['zipcode'],
		   'idcandidate' => $data['idcandidate']
		);
		
		$this->db->where('id', $data['idaddress']);
		
		return $this->db->update('candidate_address', $data_insert);
    }
    
	function delete($data)
    {
    	$data_delete = array(
		   'id' => $data['id']
		);
		
		return $this->db->delete('candidate_address', $data_delete);
    }
}
?>